﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Shine.SchemaManager
{
    public partial class ProjectItemForm : Office2007Form
    {
        private static ProjectItemForm instance = null;
        public delegate void GetProjectObj(规划项目 obj);
        public GetProjectObj getDelegateHandler = null;
        private 规划项目 project = null;

        private ProjectItemForm()
        {
            InitializeComponent();
            this.FillData();
        }

        public static ProjectItemForm GetInstance()
        {
            if (instance == null)
            {
                instance = new ProjectItemForm();
            }
            return instance;
        }

        private void FillData()
        {
            this.cbxStatus.Items.Clear();
            this.cbxStatus.Items.Add(ProjectStatus.NotStart);
            this.cbxStatus.Items.Add(ProjectStatus.NotFinish);
            this.cbxStatus.Items.Add(ProjectStatus.Finished);
            this.cbxStatus.SelectedIndex = 0;
            
            this.cbxOffice.Items.Clear();
            this.cbxOffice.Items.Add(ProjectZone.zoneA);
            this.cbxOffice.Items.Add(ProjectZone.zoneB);
            this.cbxOffice.Items.Add(ProjectZone.zoneC);
            this.cbxOffice.SelectedIndex = 0;
        }

        private void tbxModelPath_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "请选择项目……";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files(*.udd) | *.udd";
            fdlg.FilterIndex = 0;
            fdlg.RestoreDirectory = false;
            if(fdlg.ShowDialog() == DialogResult.OK)
            {
                this.tbxModelPath.Text = fdlg.FileName;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if(tbxModelPath.Text.Trim().Length == 0)
            {
                MessageBox.Show("请选择需要加载的设计方案！");
                return;
            }

            ///TODO:保存XML
            project = new 规划项目
            {
                ProjectName = tbxProjectName.Text.Length > 0 ? tbxProjectName.Text : "",
                ProjectNum = tbxNumber.Text.Length > 0 ? tbxNumber.Text : "",
                Type = tbxProjectType.Text.Length > 0 ? tbxProjectType.Text : "",
                Designer = tbxCompany.Text.Length > 0 ? tbxCompany.Text : "",
                Status = cbxStatus.SelectedItem.ToString().Length > 0 ? cbxStatus.SelectedItem.ToString() : "",
                Zone = cbxOffice.SelectedItem.ToString().Length > 0 ? cbxOffice.SelectedItem.ToString() : "",
                StartDate = dtpStartDate.Value.ToShortDateString(),
                EndDate = dtpEndDate.Value.ToShortDateString(),
                Department = tbxDepartment.Text.Length > 0 ? tbxDepartment.Text : "",
                Operator = tbxEmployee.Text.Length > 0 ? tbxEmployee.Text : "",
                Path = tbxModelPath.Text.Length > 0 ? tbxModelPath.Text : ""
            };

            // 保存XML
            SaveFileDialog fdlg = new SaveFileDialog();
            fdlg.Title = "另存为……";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files(*.xml) | *.xml";
            fdlg.FilterIndex = 0;
            fdlg.RestoreDirectory = false;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                //string fileXML = System.IO.Path.GetDirectoryName(fdlg.FileName) + "\\" + fdlg.FileName + ".xml";
                string fileXML = fdlg.FileName;
                if(!fileXML.Contains(".xml"))
                {
                    fileXML += ".xml";
                }
                Shine.Util.Configurations.ConfigurationHelper.SaveObjectToXml<规划项目>(project, fileXML, "Root");
            }

            if (this.getDelegateHandler != null)
            {
                this.getDelegateHandler(this.project);
            }

            MessageBox.Show("方案新建成功！");
            this.DefaultValues();
            this.Close();
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.DefaultValues();
        }

        private void DefaultValues()
        {
            this.tbxProjectName.Text = "";
            this.tbxNumber.Text = "";
            this.tbxProjectType.Text = "";
            this.tbxCompany.Text = "";
            this.cbxStatus.SelectedIndex = 0;
            this.cbxOffice.SelectedIndex = 0;
            this.dtpStartDate.ResetText();
            this.dtpEndDate.ResetText();
            this.tbxDepartment.Text = "";
            this.tbxEmployee.Text = "";
            this.tbxModelPath.Text = "";
        }

        private void ProjectItemForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DefaultValues();
            this.Close();
        }
    }
}
